package cn.org.timespace.easyframe.service.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.easyframe.dao.permission.EfRoleControlPointDAO;
import cn.org.timespace.easyframe.model.permission.EfControlPoint;
import cn.org.timespace.easyframe.model.permission.EfRole;
import cn.org.timespace.easyframe.model.permission.EfRoleControlPoint;
import cn.org.timespace.easyframe.service.permission.EfRoleControlPointService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;

/**
 * <p>
 * 角色控制点
 * </p>
 * 
 * @version 1.0, 11/02/27
 * @since 1.0
 * @author wubo
 */

public class EfRoleControlPointServiceImpl implements EfRoleControlPointService {

	private EfRoleControlPointDAO efRoleControlPointDao;

	public void delete(String roleId) {
		efRoleControlPointDao.delete(roleId);
	}

	public List<EfRoleControlPoint> findByCondition(String roleId) {
		return efRoleControlPointDao.findByCondition(roleId);
	}

	public void save(EfRoleControlPoint transientInstance) {
		efRoleControlPointDao.save(transientInstance);
	}

	public void saveRoleControlPoint(String roleId, String controlPointIds) {

		if (!StringUtils.isEmpty(roleId)
				&& !StringUtils.isEmpty(controlPointIds)) {
			// 先删除
			efRoleControlPointDao.delete(roleId);

			// 再保存
			String[] arrMenuIds = controlPointIds.split(",");
			for (int i = 0; i < arrMenuIds.length; i++) {
				
				EfRoleControlPoint efRoleControlPoint = new EfRoleControlPoint();
				efRoleControlPoint.setRoleControlPointId(IdentityGenerator.getInstance().getId());

				EfRole efRole = new EfRole();
				efRole.setRoleId(roleId);
				efRoleControlPoint.setEfRole(efRole);

				EfControlPoint controlPoint = new EfControlPoint();
				controlPoint.setControlPointId(arrMenuIds[i]);
				efRoleControlPoint.setEfControlPoint(controlPoint);

				efRoleControlPointDao.save(efRoleControlPoint);
			}
		}

	}

	public EfRoleControlPointDAO getEfRoleControlPointDao() {
		return efRoleControlPointDao;
	}

	public void setEfRoleControlPointDao(EfRoleControlPointDAO efRoleControlPointDao) {
		this.efRoleControlPointDao = efRoleControlPointDao;
	}

}
